package com.kaiyidiannao.model.req.model;

import android.util.Log;
import com.kaiyidiannao.data.DataCenter;
import com.kaiyidiannao.https.HttpUtils;
import com.kaiyidiannao.model.Result;
import com.kaiyidiannao.model.bean.DaydayCouponBean;
import com.kaiyidiannao.model.req.base.BaseCommand;
import com.kaiyidiannao.view.ActionBarView;
import com.taplinker.core.util.json.JsonUtil;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

/**
 *   天天惠-列表 异步请求
 */
public class GetDayDayCouponLIst extends BaseCommand {

    private int catid = DataCenter.daydaycoupon_category_id[0]; //当前分类编号
    private int page =  DataCenter.LISTVIEW_DEFAULT_PAGE; //当前页码
    private int  order = -1;//排序方式
    private String TAG = "GetDayDayCouponLIst";
    private ActionBarView actionBarView;

    public GetDayDayCouponLIst() {
    }

    public GetDayDayCouponLIst(int catid) {
        this.catid = catid;
    }

    public GetDayDayCouponLIst(int catid, int page) {
        this.catid = catid;
        this.page = page;
    }

    public GetDayDayCouponLIst(int catid, int page, int order) {
        this.catid = catid;
        this.page = page;
        this.order = order;
    }

    @Override
    public String getRequestUrl() {
        return DataCenter.GET_DAY_DAY_COUPON_LIST;
    }

    @Override
    public HashMap<String, String> getRequestParamters() {
        HashMap<String, String> paramters = new HashMap<String, String>();
        paramters.put("catid", catid  + "");
        paramters.put("page",page + "");
        if (order != -1) {
            paramters.put("order",order + "");
        }
        return paramters;
    }

    @Override
    public Result go(String url, HashMap<String, String> paramters) { // 联网时后台执行
        // 根据请求参数返回请求数据
        return HttpUtils.getByHttpClient(url, paramters);
    }

    @Override
    public void yes(Result result) { //后台执行完毕后执行
        super.yes(result);
        Log.d(DataCenter.LOG_INFO, "GetCaseComment.yes()");

        JSONArray jsonArray = null;
        try {
            Log.d(TAG, result.object.toString());
            Log.d("+++++++",DataCenter.day_position+"===");
            Log.d("+++++++",DataCenter.PAGE+"===");
            JSONObject jsonObject = new JSONObject(result.object.toString());
            jsonArray = jsonObject.optJSONArray("data");
            if (page == DataCenter.PAGE) {
                for (int i = DataCenter.daydayCouponBeanList.size()-1; i >= (DataCenter.day_position/10+1)*10-10; i--) {
                    DataCenter.daydayCouponBeanList.remove(i);
                }
            }
            ArrayList<DaydayCouponBean> targetList = DataCenter.daydayCouponBeanList;
            if (page == DataCenter.LISTVIEW_DEFAULT_PAGE) {
                targetList.clear();
            }

            for(int i = 0; i < jsonArray.length(); ++i){ // i从0开始，屏蔽提示信息
                JSONObject o = jsonArray.getJSONObject(i);
                DaydayCouponBean bean = JsonUtil.jsonToObject(o.toString(), DaydayCouponBean.class);
                targetList.add(bean);
            }
            Log.d(TAG, targetList.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }

        DataCenter.getInstance().callBacks(DataCenter.flag.GET_DAY_DAY_COUPON_LIST_SUCCESS);

        if (jsonArray != null && jsonArray.length() < 1) {
            DataCenter.getInstance().callBacks(DataCenter.flag.GET_PLAY_LIST_NO_MORE_DATA);
        }
    }

    @Override
    public void no(Result result) {
        super.no(result);
        Log.d(DataCenter.LOG_ERROR, "GetDayDayCouponLIst.no()");

//        DataCenter.daydayCouponBeanList.clear();
        DataCenter.getInstance().callBacks(DataCenter.flag.GET_DAY_DAY_COUPON_LIST_FAILED);

    }
}